package com.uenpay.oem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uenpay.oem.MyApplication;
import com.uenpay.oem.R;
import com.uenpay.oem.data.bean.TransPayType;
import com.uenpay.oem.data.bean.WrapPayType;
import com.uenpay.oem.util.BankCardUtil;
import com.uenpay.service.bean.BankCardInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: liulou
 * Date: 2017/6/26.
 * desc:
 */

public class ChoosePayTypeAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<WrapPayType> mList;
    private RecharageAdapter.OnBankItemClickListener listener;
    private int choosePosition = -1;

    public ChoosePayTypeAdapter(Context context, List<WrapPayType> list) {
        this.mContext = context;
        this.mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_recharge_dialog, null);
        return new ChoosePayTypeAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChoosePayTypeAdapter.ViewHolder mHolder = (ViewHolder) holder;
        mHolder.setPosition(position);

        if (position == choosePosition) {
            mHolder.itemBingo.setVisibility(View.VISIBLE);
        } else {
            mHolder.itemBingo.setVisibility(View.INVISIBLE);
        }
        TransPayType type = mList.get(position).getType();
        if (type == TransPayType.BALANCE) {
            setBalanceViews(mHolder);
        } else if (type == TransPayType.BANKCARD) {
            setBankCardViews((BankCardInfo) (mList.get(position).getObject()), mHolder);
        } else if (type == TransPayType.ALIPAY) {
            setWeiXinViews(mHolder);
        } else if (type == TransPayType.WEIXIN) {
            setAliPayViews(mHolder);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private int position;
        @BindView(R.id.itme_iv)
        ImageView itmeIv;
        @BindView(R.id.item_bingo)
        ImageView itemBingo;
        @BindView(R.id.item_text)
        TextView textView;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.chooseBank(position);
                        choosePosition = position;
                    }
                }
            });


            /*for (int i = 0; i < mBankCards.size(); i++) {
                if (i==position){
                    itemBingo.setVisibility(View.VISIBLE);
                }else {
                    itemBingo.setVisibility(View.GONE);
                }
            }*/

        }

        public void setPosition(int position) {
            this.position = position;
        }


    }

    public void setChoosePosition(int position) {
        choosePosition = position;
        notifyDataSetChanged();
    }

    public interface OnBankItemClickListener {
        void chooseBank(int position);
    }

    public void setOnBankItemClickListener(RecharageAdapter.OnBankItemClickListener listener) {
        this.listener = listener;
    }

    private void setBalanceViews(ChoosePayTypeAdapter.ViewHolder holder) {
        holder.textView.setText("余额（¥" + MyApplication.getBalance() + ")");
        holder.itmeIv.setImageResource(R.drawable.ic_paytype_balance);
    }

    private void setBankCardViews(BankCardInfo cardInfo, ChoosePayTypeAdapter.ViewHolder holder) {
        holder.textView.setText(cardInfo.getBankName() + "(" + BankCardUtil.getLast4NumOfBankCard(cardInfo.getCardNo()) + ")");
        holder.itmeIv.setImageResource(BankCardUtil.getLogoByBankName(cardInfo.getBankName()));
    }

    private void setWeiXinViews(ChoosePayTypeAdapter.ViewHolder holder) {
        holder.textView.setText(R.string.paytype_weixin);
        holder.itmeIv.setImageResource(R.drawable.ic_alipay_logo);
    }

    private void setAliPayViews(ChoosePayTypeAdapter.ViewHolder holder) {
        holder.textView.setText(R.string.paytype_alipay);
        holder.itmeIv.setImageResource(R.drawable.ic_alipay_logo);
    }

}
